<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="活动板块:">
        <el-select v-model="queryParams.panelId" size="small" @change="handlePanelChange">
          <el-option v-for="option in options.panelOptions" :key="option.id" :label="option.panelName" :value="option.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动板块" align="center" prop="panelName" />
      <el-table-column label="内容名称" align="center" prop="contentName" />
      <el-table-column label="内容类型" align="center" prop="contentType">
        <template slot-scope="scope">
          <span>{{ contentTypeFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片地址" align="center" prop="image" />
      <el-table-column label="图片地址(大)" align="center" prop="bigImage" />
      <el-table-column label="链接地址" align="center" prop="fullUrl" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['marketing:panel:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['marketing:panel:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="panelContentFormDialog.title" :visible.sync="panelContentFormDialog.visible" :close-on-click-modal="false" :before-close="resetPanelContentFormDialog" width="500px" append-to-body>
      <el-form ref="panelContentForm" :model="panelContentFormData" :rules="panelContentFormRules" label-width="100px">
        <el-form-item label="内容类型" prop="contentType">
          <el-select v-model="panelContentFormData.contentType" style="width: 100%" clearable placeholder="请选择内容类型">
            <el-option v-for="option in options.contentTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="panelContentFormData.contentType !== 'goods' && panelContentFormData.contentType !== 'special_goods'" label="内容名称" prop="image">
          <el-input v-model="panelContentFormData.contentName" placeholder="请输入内容名称" />
        </el-form-item>
        <el-form-item v-if="panelContentFormData.contentType === 'goods' || panelContentFormData.contentType === 'special_goods'" label="内容名称">
          <el-popover placement="bottom-start" width="460" trigger="click" @show="$refs['spuSelector'].reset()">
            <spu-selector ref="spuSelector" @selected="handleSelectedGoods($event)" />
            <el-input slot="reference" v-model="panelContentFormData.contentName" placeholder="点击选择商品" readonly />
          </el-popover>
        </el-form-item>
        <el-form-item label="图片地址" prop="image">
          <el-input v-model="panelContentFormData.image" placeholder="请输入图片地址" />
        </el-form-item>
        <el-form-item label="图片地址(大)" prop="image">
          <el-input v-model="panelContentFormData.bigImage" placeholder="请输入图片地址(大)" />
        </el-form-item>
        <el-form-item label="链接地址" prop="fullUrl">
          <el-input v-model="panelContentFormData.fullUrl" placeholder="请输入链接地址" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="panelContentFormData.sort" type="number" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmPanelContentFormDialog">确 定</el-button>
        <el-button @click="resetPanelContentFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PanelContentApi from '@/api/marketing/PanelContentApi'
import PanelApi from '@/api/marketing/PanelApi'
import DictValueApi from '@/api/system/DictValueApi'
import SpuSelector from '@/views/goods/spu/SpuSelector'

const defaultPanelContentFormData = {
  panelId: null,
  panelName: null,
  contentType: null,
  contentName: null,
  goodsId: null,
  goodsName: null,
  image: null,
  bigImage: null,
  fullUrl: null,
  sort: 10
}
export default {
  name: 'PanelContentList',
  components: {
    SpuSelector
  },
  data() {
    return {
      options: {
        panelOptions: [],
        contentTypeOptions: [],
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        panelId: null
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      panelContentFormDialog: {
        title: '',
        visible: false
      },
      panelContentFormData: Object.assign({}, defaultPanelContentFormData),
      panelContentFormRules: {
        contentType: [{ required: true, message: '内容类型不能为空', trigger: 'change' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    const panelId = this.$route.params && this.$route.params.panelId
    PanelApi.get(panelId).then(response => {
      this.queryParams.panelId = response.data.id
      defaultPanelContentFormData.panelId = response.data.id
      defaultPanelContentFormData.panelName = response.data.panelName
      this.panelContentFormData.panelId = response.data.id
      this.panelContentFormData.panelName = response.data.panelName
      this.handleQuery()
    })
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      PanelApi.list().then(response => {
        this.options.panelOptions = response.data
      })
      DictValueApi.list({ dictCode: 'mms_panel_content_type' }).then(response => {
        this.options.contentTypeOptions = response.data
      })
    },
    handleAdd() {
      this.openPanelContentFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return PanelContentApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openPanelContentFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      PanelContentApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return PanelContentApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openPanelContentFormDialog(id) {
      this.panelContentFormDialog.visible = true
      if (id) {
        this.panelContentFormDialog.title = '活动板块内容-修改'
        PanelContentApi.get(id).then(response => {
          this.panelContentFormData = {
            ...this.panelContentFormData,
            ...response.data
          }
        })
      } else {
        this.panelContentFormDialog.title = '活动板块内容-添加'
      }
    },
    confirmPanelContentFormDialog() {
      this.$refs['panelContentForm'].validate(valid => {
        if (valid) {
          if (this.panelContentFormData.id) {
            PanelContentApi.edit(this.panelContentFormData).then(response => {
              this.$message.success('修改成功')
              this.resetPanelContentFormDialog()
              this.handleQuery()
            })
          } else {
            PanelContentApi.add(this.panelContentFormData).then(response => {
              this.$message.success('添加成功')
              this.resetPanelContentFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetPanelContentFormDialog() {
      this.panelContentFormDialog = {
        title: '',
        visible: false
      }
      this.panelContentFormData = Object.assign({}, defaultPanelContentFormData)
      this.resetForm('panelContentForm')
    },
    handleSelectedGoods(obj) {
      this.panelContentFormData.goodsId = obj.id
      this.panelContentFormData.contentName = obj.spuName
      this.panelContentFormData.goodsName = obj.spuName
      this.panelContentFormData.title = obj.title
      this.panelContentFormData.price = obj.price
      this.panelContentFormData.image = obj.image
      this.panelContentFormData.bigImage = obj.image
      console.log(this.panelContentFormData)
    },
    handlePanelChange(panelId) {
      PanelApi.get(panelId).then(response => {
        this.queryParams.panelId = response.data.id
        defaultPanelContentFormData.panelId = response.data.id
        defaultPanelContentFormData.panelName = response.data.panelName
        this.panelContentFormData.panelId = response.data.id
        this.panelContentFormData.panelName = response.data.panelName
        this.handleQuery()
      })
    },
    // 内容类型字典
    contentTypeFormat(row) {
      return this.$common.getDictValueName(this.options.contentTypeOptions, row.contentType)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
